<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/6
 * Time: 16:56
 */

namespace App\Models\Common;


use App\Libraries\Weapp\WeappLib;
use App\Models\BaseModel;

class MsdMember extends BaseModel
{

    protected $table = 'msd_member';

    //获取用户的信息
    public static function getWechatInfo($data)
    {
        //获取session_key用于交换用户信息
        $session_key = WeappLib::getSessionKey($data['code']);
        //获取用户信息
        $result = WeappLib::decryptData($session_key['session_key'],$data['iv'],$data['data']);
        return $result;
    }

    //添加用户
    public static function addUser($data)
    {
        $datas = self::getWechatInfo($data);
        $union_id = self::where('union_id',$datas['unionId'])->first();
        if($union_id){
            return $union_id;
        }
        $install = array(
            'name' => $datas['nickName'],
            'mini_openid' => $datas['openId'],
            'union_id' => $datas['unionId'],
            'nick_name' => $datas['nickName'],
            'header_pic' => $datas['avatarUrl'],
            'sex' => $datas['gender'],
        );
        $result = static::create($install);
        return $result;
    }

}